package com.gebogebo.android.justads;

import static java.lang.String.format;

import android.util.Log;

import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest.ErrorCode;

/**
 * listener class for ad mob ad events
 * 
 * @author viraj
 */
public class AdMobAdListener implements AdListener {
    private final AdEventsListener listener;

    /**
     * constructor accepting ad event listener for this app
     * 
     * @param listener listener to which admob ad events will be published to 
     */
    public AdMobAdListener(AdEventsListener listener) {
        this.listener = listener;
    }

    /* (non-Javadoc)
     * @see com.google.ads.AdListener#onDismissScreen(com.google.ads.Ad)
     */
    public void onDismissScreen(Ad arg0) {
        //nothing to do
    }

    /* (non-Javadoc)
     * @see com.google.ads.AdListener#onFailedToReceiveAd(com.google.ads.Ad, com.google.ads.AdRequest.ErrorCode)
     */
    public void onFailedToReceiveAd(Ad arg0, ErrorCode error) {
        Log.i("admob", format("failed to receive admob ad. error: %s", error));
        listener.adFailed();
    }

    /* (non-Javadoc)
     * @see com.google.ads.AdListener#onLeaveApplication(com.google.ads.Ad)
     */
    public void onLeaveApplication(Ad arg0) {
        //nothing to do
    }

    /* (non-Javadoc)
     * @see com.google.ads.AdListener#onPresentScreen(com.google.ads.Ad)
     */
    public void onPresentScreen(Ad arg0) {
        //nothing to do
    }

    /* (non-Javadoc)
     * @see com.google.ads.AdListener#onReceiveAd(com.google.ads.Ad)
     */
    public void onReceiveAd(Ad arg0) {
        Log.i("admob", "received admob ad");
        listener.adReceived();
    }
}
